var mlen = 1000;
var NUM_PARTICLES = mlen;
var arr = new Array(mlen);
var distance = new Array(mlen);//相关粒子
var orgv2 = new Array(mlen);//能量
var { initPointList } = require("../../points")
const { Plot } = require("../../plot");
var plot = new Plot(document.querySelector("#myCanvas"));
var list = initPointList(NUM_PARTICLES);
var tog = false;
var DRAG = 1;
//let grev=2.8,repu=0.2; var enum1=5; //配置1
// let grev=2.8,repu=0.2; var enum1=50000;
// window.grev=2.8,window.repu=5; window.enum1=-1.5;
window.grev = 0.3, window.repu = 1.5; window.enum1 = 500000;
function step() {
  // if (stats) stats.begin();
  if (tog = !tog) {
    for (i = 0; i < NUM_PARTICLES; i++) {
      let p = list[i];
      var pv2 = p.vx * p.vx + p.vy * p.vy;// 当前p 的动能2
      var sumd = 0;
      for (let j = 0, length2 = mlen; j < length2; j++) {
        arr[j] = Math.floor((Math.random() * NUM_PARTICLES));
        var tem = list[arr[j]];
        distance[j] = Math.sqrt((tem.x - p.x) * (tem.x - p.x) + (tem.y - p.y) * (tem.y - p.y));
        if (distance[j] < 0.00001 || isNaN(distance[j])) distance[j] = 0.00001;//最小距离
        orgv2[j] = tem.vx * tem.vx + tem.vy * tem.vy;
        sumd += distance[j];
      }
      if (isNaN(sumd)) sumd = 100;
      if (pv2 > 10000) {// 能量转移阙值
        //rate=0.36
        p.vx = 0.8 * p.vx;// 损失36%
        p.vy = 0.8 * p.vy;
        var k = 0.36 * pv2 / sumd * mlen;//转移动能
        for (let j = 0, length2 = mlen; j < length2; j++) {
          //if(k>10)
          if (orgv2[j] != 0 && !isNaN(orgv2[j])) {
            /* if(orgv2[j]<pv2){*/
            let tmpk = Math.sqrt(k / distance[j] / orgv2[j]);
            list[arr[j]].vx = Math.sqrt(1 + tmpk) * list[arr[j]].vx;// * (p.x - list[arr[j]].x) / distance[j];//6.23 加法改乘法
            list[arr[j]].vy = Math.sqrt(1 + tmpk) * list[arr[j]].vy;// * (p.y - list[arr[j]].y) / distance[j];
            //   }
          }
          else if (isNaN(orgv2[j])) {//||orgv2[j]<20
            let r = Math.random() * 2 * Math.PI
            let dv1 = Math.sqrt(k / distance[j]);
            list[arr[j]].vx = dv1 * Math.cos(r) * 10;
            list[arr[j]].vy = dv1 * Math.sin(r) * 10;
            // console.log("vx"+list[arr[j]].vx+":vy"+list[arr[j]].vy );
          }
        }
      }
      else {
        let r = Math.random() * 2 * Math.PI;
        p.vx += Math.cos(r);
        p.vy += Math.sin(r);
      }
      //
      /* var tem1 = list[arr[j]];
        k-=( tem1.vx * tem1.vx + tem1.vy * tem1.vy-orgv2[j]);//*/
      for (let j = 0, length2 = mlen; j < length2; j++) {
        let pv = { dvx: p.x - list[arr[j]].x, dvy: p.y - list[arr[j]].y };
        let dist1 = distance[j];
        if ((distance[j] > 100)) {
          list[arr[j]].vx += pv.dvx / dist1 * grev;//* Math.sin(t) * Math.sin(t)
          list[arr[j]].vy += pv.dvy / dist1 * grev;
        } else if (distance[j] < 100) {//
          list[arr[j]].vx -= pv.dvx / dist1 * repu;//4//
          list[arr[j]].vy -= pv.dvy / dist1 * repu;//4//
        }
      }
      p.x += (p.vx *= DRAG) //+ (p.ox - p.x) * EASE;//Math.sin(t)*Math.sin(t);
      p.y += (p.vy *= DRAG) //+ (p.oy - p.y) * EASE;//Math.cos(t)*Math.cos(t);
    }

  } else {
    plot.drawList(list)

  }
}
step();
setInterval(step, 10);
//完成随机 能量转移  drawa=draw;draw=function(){} ；setInterval(drawa,5000)